//
//  FANVisitorView.swift
//  Weibo - Swift
//
//  Created by user on 16/4/16.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit


@objc protocol FANVisitorViewDelegate:NSObjectProtocol{
    
    func didRegisterButton()
    
    func didLoginButton()

    
}

class FANVisitorView: UIView {
    
    weak var delegate:FANVisitorViewDelegate?

    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var circleImageView:UIImageView = {
    
        let imageView = UIImageView()
        
        imageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        imageView.hidden = true
        
        return imageView
    
    }()
    
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel(textColor: UIColor.blackColor(), fontSize: 15 ,preferredMaxLayoutWidth:250)
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        label.textAlignment = .Center
        
        return label
        
    }()
    
    private lazy var registerButton:UIButton = {
       
        let button = UIButton(titleColor: UIColor.orangeColor(), fontSize: 15)
        
        button.setBackgroundImage(UIImage(named:"common_button_white"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didRegisterButton", forControlEvents: .TouchUpInside)
        
        return button
        
    }()
    
    private lazy var loginButton:UIButton = {
        
       let button = UIButton(titleColor:UIColor.orangeColor(), fontSize: 15)
        
        button.setBackgroundImage(UIImage(named:"common_button_white"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didLoginButton", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    @objc func didRegisterButton(){
        
//        print("点击了注册按钮")
        
        delegate?.didRegisterButton()
    }
    
    @objc func didLoginButton(){
        
//        print("点击了登录按钮")
        delegate?.didLoginButton()
        
    }
    
    private lazy var coverImageView:UIImageView = {
        
       let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imageView
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
     
        setupUI()
    }
    
    func loadSomeInfo(imageName:String? , textTitle:String?){
        
        if let imageN = imageName, title = textTitle{
            
            self.iconImageView.image = UIImage(named:imageN)
            
            self.messageLabel.text = title
            
        }else{
            
            startAnim()
            
        }
        
    }
    
    private func startAnim(){
        
        self.circleImageView.hidden = false
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 18
        
        anim.removedOnCompletion = false
        
        self.circleImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        addSubview(circleImageView)
        addSubview(coverImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        circleImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleImageView, attribute: .Bottom, multiplier: 1, constant: 15))
        
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        coverImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .CenterX, relatedBy: .Equal, toItem: circleImageView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .Bottom, relatedBy: .Equal, toItem: circleImageView, attribute: .Bottom, multiplier: 1, constant: 0))
        
        
    }
    

}
